package com.xj;

import com.xj.server.ReadfileService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class OpenapiAddMenu {

@Autowired
ReadfileService readfileService;
    @Test
    public void test() throws Exception {
        readfileService.redaFile();
    }

    @Test
    public void outputFile() throws Exception {
       readfileService.outputFile();

    }


    @Test
    public  void  test1()  throws  Exception{
        String[]  command=   {"net","stop","redis" };
        Runtime runtime = Runtime.getRuntime();
        List<String> commands = new ArrayList<>();
        commands.addAll(Arrays.asList("cmd", "/c"));
        commands.add(String.join(" ", command));
        int i=0;
        while(i<5) {
            Process process = runtime.exec(commands.toArray(new String[0]));
            Thread.sleep(2000);
            PrintWriter out = new PrintWriter(process.getOutputStream());
            String cmd = "Y"; //你的cmd命令
            out.write(cmd); //输入你的命令
            out.flush(); //写到控制台
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
            String line;
            while ((line = reader.readLine()) != null) {
                log.info(line);
            }
            process.waitFor();
            int exited = process.exitValue();
            log.info("第{}次执行stop  ufnet ,result={}",i, exited);
            if(exited==0){
                break;
            }
            i++;
        }
    }




}
